package org.jwcloud.project.module.zx.common;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Stopwatch;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @Description //请求参数aop
 **/
@Component
@Aspect
@Slf4j
public class RequestParameterAop { 

	static {
		System.out.println("aop--------------------------");
	}
//    /**
//     * @Description: 定义需要拦截的切面
//     * @Pointcut("execution(* com.*.controller.*Controller.*(..))")
//     * @Return: void
//     **/
//    @Pointcut("execution(* org.jwcloud.**.*(..))")
//    public void methodArgs() {
//
//    }

    //@Pointcut("execution(* org.jwcloud.**.*(..))")
    
    @Pointcut("execution(* org.jwcloud.project.module.zx..*.*(..))")
    private void webLog(){

    }
    @Before(value="webLog()")
    public void before(JoinPoint joinPoint){
    	System.out.println("go in aop...............");
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(requestAttributes == null) {
//        	System.out.println("aop is null---------------------");
        	return;
        }
        
        HttpServletRequest request = requestAttributes.getRequest();
        
//        System.out.println("access_token: -------- " + request.getHeader("access_token"));
        
//        System.out.println("-------------------------------------");
//        log.info("验证请求日志的打印");
//        log.info("请求地址:{}", Optional.ofNullable(request.getRequestURI().toString()).orElse(null));
//        log.info("请求方式:{}",request.getMethod());
        System.out.println("请求类方法:{}"+joinPoint.getSignature());
        System.out.println("请求类方法参数:{}"+ Arrays.toString(joinPoint.getArgs()));
        Object[] args = joinPoint.getArgs();
        if(args == null || args.length == 0) {
        	return;
        }
        
        for(Object argObj: args) {
        	if(argObj instanceof Map) {
        		Map temp = (Map) argObj;
//        		temp.put("_session_user", "test_user");
//        		temp.put("_session_dept", "test_dept");
//        		temp.put("_session_org", "test_org");
        		
//        		System.out.println("合成前参数：" + temp);
        		
        		if(!temp.containsKey("_session_user")) 
        			temp.put("_session_user", request.getHeader("_session_user"));
        		
        		if(!temp.containsKey("_session_dept")) 
        			temp.put("_session_dept", request.getHeader("_session_org"));
        		
        		if(!temp.containsKey("_session_org")) 
        			temp.put("_session_org", request.getHeader("_session_org"));
        		
        		temp.put("_chengjian_dept_id", "7824");
        		
        		if("7824".equals(request.getHeader("_session_org")) || request.getHeader("_session_org") == null) {
        			temp.put("_session_role_isJw", "0");//城建
        		}else {
        			temp.put("_session_role_isJw", "1");//纪委
        		}
        		
        		System.out.println("合成后参数：" + temp);
        	}
        }
        
        System.out.println("请求时间：" + new Date());
        
    }
//    @AfterReturning(returning = "o",pointcut = "webLog()")
//    public void after(Object o){
//        log.info("返回的内容:{}","1233");
//    }


    
    
}
